package com.easy.work.function.me.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.andview.refreshview.utils.Utils;
import com.base.syslib.util.DensityUtil;
import com.bumptech.glide.Glide;
import com.easy.work.R;

import java.io.File;
import java.util.List;

public class PictureAdapter extends ArrayAdapter<String> {

    private List<String> mList;
    private LayoutInflater mInflater;
    private Context mContext;

    private int height;

    public PictureAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        mList = objects;
        mContext = context;
        mInflater = LayoutInflater.from(context);
        height = (Utils.getScreenWidth(context) - DensityUtil.dip2px(context, 50)) / 4;
    }

    public List<String> getList() {
        return mList;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_pic, parent, false);
            holder = new ViewHolder();
            holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_picture);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.ivPic.getLayoutParams();
        lp.height = height;
        holder.ivPic.setLayoutParams(lp);
        if (position == mList.size() - 1) {
            Glide.with(mContext)
                    .load("")
                    .error(R.drawable.tjtp)
                    .thumbnail(0.1f)
                    .into(holder.ivPic);
        } else {
            Uri uri = Uri.fromFile(new File(mList.get(position)));
            Glide.with(mContext)
                    .load(uri)
                    .thumbnail(0.1f)
                    .into(holder.ivPic);
        }
        return convertView;
    }


    class ViewHolder {
        ImageView ivPic;
    }

}
